package com.yuntu.chapter6;

import java.util.Scanner;

/**
 * @author MrWang-shuizhishuiheshui
 * @description dvd项目
 */
public class DvdProject {
    public static void main(String[] args) {
        //创建扫描仪
        Scanner input=new Scanner(System.in);
        //数据初始化 -- 创建数组 ; 默认数组中的数据信息
        String[] names = {"哈哈","呵呵","嘿嘿",null,null,null};
        String[] states = {"借出","可借","可借",null,null,null};
        int[] days = {1,0,0,0,0,0};
        int[] nums = {10,20,30,0,0,0};
        //创建一个用于控制循环跳出的变量
        boolean flag = true;
        //循环条件变量
        int result = -1;
        //由于题意中先运行循环内容,再进行条件判断,选择do-while
        do {
            System.out.println("*************************");
            System.out.println("-------------------------");
            System.out.println("1.新增");
            System.out.println("2.查看");
            System.out.println("3.删除");
            System.out.println("4.借出");
            System.out.println("5.归还");
            System.out.println("6.退出");
            System.out.println("-------------------------");
            System.out.println("*************************");
            System.out.println("请选择:");
            int answer=input.nextInt();
            //使用switch进行菜单选项的等值比较
            switch (answer){
                case 1:
                    System.out.println("-->新增");
                    System.out.println("请输入你的DVD名称:");
                    String dvdName=input.next();
                    //创建变量判断是否满
                    boolean man=true;
                    //循环找到能够插入dvd的位置
                    for (int i = 0; i < names.length; i++) {
                        //判断dvd名称数组中为null的位置
                        if (names[i]==null){
                            names[i]=dvdName;
                            states[i]="可借";
                            man=false;
                            break;
                        }
                    }
                    //处理dvd全部都满的情况
                    if (man){
                        System.out.println("添加"+dvdName+"失败,仓库已满");
                    }

                    break;
                case 2:
                    System.out.println("-->查看");
                    System.out.println("序号\t状态\t名称\t借出日期\t借出次数");
                    for (int i = 0; i < names.length; i++) {
                        //判断名称不为null的时候展示信息
                        if (names[i]!=null){
                            /*
                            条件 ? 结果1 : 结果2 ;
                            条件成立,取结果1
                            条件不成立,取结果2
                             */
                            String day = days[i]==0 ? "\t" : days[i]+"日";

                            System.out.println((i+1)+"\t"+states[i]+"\t"+names[i]+"\t"
                            +day+"\t"+nums[i]);
                        }
                        
                    }
                    break;
                case 3:
                    System.out.println("-->删除");
                    System.out.println("请输入需要删除的dvd");
                    String delName=input.next();
                    //创建查找变量
                    boolean find=false;
                    //循环查找删除的dvd
                    for (int i = 0; i < names.length; i++) {
                        if (names[i]!=null && delName.equals(names[i]) && states[i].equals("借出")){
                            System.out.println("该dvd已经借出,无法删除");
                            find=true;
                            break;
                        }else if (names[i]!=null && delName.equals(names[i]) && states[i].equals("可借")){
                            find=true;
                            //可删状态
                            //循环从i的位置依次将元素前移
                            for (int j = i; j < names.length; j++) {
                                //判断不是最后一位
                                if(j!=names.length-1){
                                    names[j]=names[j+1];
                                    states[j]=states[j+1];
                                    days[j]=days[j+1];
                                    nums[j]=nums[j+1];
                                }
                            }
                            names[names.length-1]=null;
                            states[names.length-1]=null;
                            days[names.length-1]=0;
                            nums[names.length-1]=0;
                            System.out.println("删除成功");
                        }

                    }
                    if (!find){
                        System.out.println("没找到dvd");
                    }

                    break;
                case 4:
                    System.out.println("-->借出");
                    System.out.println("请输入借出的dvd");
                    String wantName=input.next();
                    //循环查找判断
                    for (int i = 0; i < names.length; i++) {
                        //判断是否有当前dvd
                        if (names[i]==null){
                            System.out.println("找不到当前dvd");
                            break;
                        }else if (names[i].equals(wantName)&&states[i].equals("可借")){
                            System.out.println("请输入借出的日期:");
                            int day=input.nextInt();
                            while(day<1 || day>31){
                                System.out.println("请输入正确日期:(1~31)");
                                day=input.nextInt();
                            }
                            states[i]="借出";
                            days[i]=day;
                            nums[i]++;
                            System.out.println("借出成功");
                            break;
                        }else if (names[i].equals(wantName)&&states[i].equals("借出")){
                            System.out.println("不好意思"+wantName+"已经借出!");
                            break;
                        }
                    }

                    break;
                case 5:
                    System.out.println("-->归还");
                    System.out.println("请输入归还的dvd");
                    String backName=input.next();
                    //循环判断
                    for (int i = 0; i < names.length; i++) {
                        if (names[i]==null){
                            System.out.println("找不到当前dvd");
                            break;
                        }else if (names[i].equals(backName)&&states[i].equals("借出")){
                            System.out.println("请输入归还日期");
                            int day=input.nextInt();
                            //循环排出不合理归还日期
                            while(day>31 || day<days[i]){
                                System.out.println("请输入正确日期:(大于等于借出日期,小于31)");
                                day=input.nextInt();
                            }
                            states[i]="可借";
                            int money=day-days[i]+1;//钱
                            //结完账,再讲借出日期清0
                            days[i]=0;
                            System.out.println("归还成功");
                            System.out.println("花费:"+money+"元");
                            break;
                        }else if (names[i].equals(backName)&&states[i].equals("可借")){
                            System.out.println("不好意思"+backName+"没有借出!");
                            break;
                        }

                    }


                    break;
                case 6:
                    System.out.println("退出系统");
                    //将退出信息传送到循环的while上面代码处,进行调处循环
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误,退出系统");
                    flag = false;
                    break;
            }

            //判断跳出的条件是否成立
            if (flag){
                System.out.println("输入0返回:");
                result=input.nextInt();
            }else{
                break;//此时跳出循环
            }
            //条件为0 , 继续循环
        }while(result==0);
        System.out.println("谢谢使用");

    }
}
